package com.weixing.mall.provider.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.SysLog;
import com.weixing.mall.core.enums.OperateEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.model.enums.AuditDeptEnum;
import com.weixing.mall.provider.model.enums.OrderAuditStateEnum;
import com.weixing.mall.provider.model.enums.OrderStateEnum;
import com.weixing.mall.provider.model.enums.ProductStatusEnum;
import com.weixing.mall.provider.model.excel.*;
import com.weixing.mall.provider.model.query.AfterSaleProblemQuery;
import com.weixing.mall.provider.model.query.CustomerOrderQuery;
import com.weixing.mall.provider.model.query.OrderPCQuery;
import com.weixing.mall.provider.model.vo.AfterSaleProblemVo;
import com.weixing.mall.provider.model.vo.KefuAuditOrderVo;
import com.weixing.mall.provider.model.vo.OrderListPCVo;
import com.weixing.mall.provider.model.vo.OutOfStockCancelOrderVo;
import com.weixing.mall.provider.service.*;
import com.weixing.mall.provider.util.EasyExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台订单相关Excel
 */
@Api(value = "后台订单Excel",tags = "后台订单Excel")
@RestController
@RequestMapping("orderExcel")
public class OrderExcelController extends BaseController {

    @Autowired
    private IOrderService orderService;
    @Autowired
    private IOrderPCService orderPCService;
    @Autowired
    private IOrderExcalService orderExcalService;
    @Autowired
    private IAfterSaleService afterSaleService;
    @Autowired
    private IKefuOrderService kefuOrderService;

    @ApiOperation(value = "PC端整体订单Excel")
    @GetMapping("selectListPCExcel")
    @SysLog(type = OperateEnum.E,desp = "整体订单导出")
    public void selectListPC(OrderPCQuery orderPCQuery, HttpServletResponse response) {
        IPage<OrderListPCVo> list = orderPCService.selectListPC(orderPCQuery,initMpPage().setSize(-1));
        try {
            EasyExcelUtil.writeExcel(response, OrderListPCED.class,orderExcalService.switchTypeOrderListPCED(list.getRecords()),"订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "问题订单列表Excel")
    @GetMapping("selectAfterSaleProblemListExcel")
    @SysLog(type = OperateEnum.E,desp = "问题订单列表导出")
    public void selectAfterSaleProblemList(AfterSaleProblemQuery afterSaleProblemQuery,HttpServletResponse response) {
        IPage<AfterSaleProblemVo> list = afterSaleService.selectAfterSaleProblemList(initMpPage().setSize(-1),afterSaleProblemQuery);
        try {

            EasyExcelUtil.writeExcel(response, AfterSaleProblemED.class,orderExcalService.switchTypeAfterSaleProblemED(list.getRecords()),"问题订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "取消订单列表Excel")
    @GetMapping("canceldListExcel")
    @SysLog(type = OperateEnum.E,desp = "取消订单列表导出")
    public void cancelList(CustomerOrderQuery query,HttpServletResponse response) {
        //调整为货品取消状态
        query.setItemStatus(ProductStatusEnum.STATUS7);
        IPage<KefuAuditOrderVo> list = kefuOrderService.toAudit(initMpPage().setSize(-1), query);
        try {
            EasyExcelUtil.writeExcel(response, KefuAuditOrderED.class,orderExcalService.switchTypeKefuAuditOrderED(list.getRecords()),"取消订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "断货订单列表Excel")
    @GetMapping("selectOutOfStockListExcel")
    @SysLog(type = OperateEnum.E,desp = "断货订单列表导出")
    public void outOfStock(@SysInfo SysAuth auth, CustomerOrderQuery customerOrderQuery,HttpServletResponse response) {
        IPage<OutOfStockCancelOrderVo> list = orderService.selectOutOfStockList(initMpPage().setSize(-1), customerOrderQuery, auth.getUid());
        try {
            EasyExcelUtil.writeExcel(response, OutOfStockCancelOrderED.class,orderExcalService.switchTypeOutOfStockCancelOrderED(list.getRecords()),"断货订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 方法描述: 客服部/订单查看/{订单列表,正价订单列表，秒杀活动订单列表，代理商活动订单列表}
     *      * 订单查看  orderType 不传
     *      * 正价订单  orderType 传 1
     *      * 代理商活动订单  orderType 传 2
     *      * 秒杀活动订单  orderType 传 3
     */
    @ApiOperation(value = "订单列表（客服部订单查看里面的）(APP仓 发货部都有用到)Excel")
    @GetMapping("listExcel")
    @SysLog(type = OperateEnum.E,desp = "客服部/订单查看/{订单列表,正价订单列表，秒杀活动订单列表，代理商活动订单列表导出")
    public void orderList(CustomerOrderQuery query,HttpServletResponse response) {
        IPage<KefuAuditOrderVo> list = kefuOrderService.selectOrderList(query, initMpPage().setSize(-1));
        try {
            EasyExcelUtil.writeExcel(response, KefuAuditOrderAllED.class,orderExcalService.switchTypeKefuAuditOrderAllED(list.getRecords()),"订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "已封单或待封单订单查询-（排除外采仓）Excel")
    @GetMapping("sealedBillOrderExcel")
    @SysLog(type = OperateEnum.E,desp = "已封单或待封单订单列表导出")
    public void sealedBillOrderExcal(OrderPCQuery orderQuery,HttpServletResponse response) {
        IPage<OrderListPCVo> list = orderService.historyOrder(initMpPage().setSize(-1), orderQuery);
        try {
            EasyExcelUtil.writeExcel(response, OrderListPCED.class,orderExcalService.switchTypeOrderListPCED(list.getRecords()),"订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "查询已发货订单分配列表Excel")
    @GetMapping("selectDeliverOrderListExcel")
    @SysLog(type = OperateEnum.E,desp = "已发货订单分配列表导出")
    public void selectDeliverOrderList(OrderPCQuery orderPCQuery,HttpServletResponse response) {
        IPage<OrderListPCVo> list = orderService.selectDeliverOrderList(orderPCQuery, initMpPage().setSize(-1));
        try {
            EasyExcelUtil.writeExcel(response, OrderListPCED.class,orderExcalService.switchTypeOrderListPCED(list.getRecords()),"已发货订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 方法描述:  后台客服部/订单审核/{待审核订单、正价待审核订单、代理商活动待审核订单、秒杀活动待审核订单}查询接口
     *
     * 待审核订单  orderType 不传
     * 正价待审核订单  orderType 传 1
     * 代理商活动待审核订单  orderType 传 2
     * 秒杀活动待审核订单  orderType 传 3
     */
    @ApiOperation(value = "客服待审核订单Excel")
    @GetMapping("kefu/toAuditExcel")
    @SysLog(type = OperateEnum.E,desp = "客服待审核订单列表导出")
    public void toAuditExcel(@SysInfo SysAuth auth, CustomerOrderQuery query,HttpServletResponse response) {
        query.setAuditDept(AuditDeptEnum.SERVICE_DEPT);
        List<Integer> states = Lists.newArrayList();
        states.add(OrderStateEnum.HAS_PAY.getValue());
        states.add(OrderStateEnum.AUDITING.getValue());
        query.setOrderStates(states);
        IPage<KefuAuditOrderVo> list =  kefuOrderService.toAudit(initMpPage().setSize(-1), query);
        try {
            EasyExcelUtil.writeExcel(response, KefuAuditOrderED.class,orderExcalService.switchTypeKefuAuditOrderED(list.getRecords()),"订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 方法描述:后台客服部/订单审核/{正价已审核订单、代理商活动已审核订单、秒杀活动已审核订单}查询接口
     *
     * 待审核订单  orderType 不传
     * 正价已审核订单  orderType 传 1
     * 代理商活动已审核订单  orderType 传 2
     * 秒杀活动已审核订单  orderType 传 3
     */
    @ApiOperation(value = "客服已审核订单Excel")
    @GetMapping("kefu/hasAuditExcel")
    @SysLog(type = OperateEnum.E,desp = "客服已审核订单导出")
    public void hasAuditExcel(@SysInfo SysAuth auth, CustomerOrderQuery query,HttpServletResponse response) {
        query.setAuditStatus(OrderAuditStateEnum.KEFU_AUDITING);
        IPage<KefuAuditOrderVo> list =  kefuOrderService.toAudit(initMpPage().setSize(-1), query);
        try {
            EasyExcelUtil.writeExcel(response, KefuAuditOrderED.class,orderExcalService.switchTypeKefuAuditOrderED(list.getRecords()),"订单列表");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
